package com.hy.mydesktop.client.component.factory;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hy.mydesktop.client.component.meta.AbstractComponetModelEnum;
import com.hy.mydesktop.client.component.meta.DateFieldModelEnum;
import com.hy.mydesktop.client.component.meta.TextFieldModelEnum;
import com.hy.mydesktop.client.rpc.ServiceInvoker;
import com.hy.mydesktop.client.rpc.ServiceInvokerAsync;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceParameter;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceResult;

/**
 * 
 * <ul>
 * <li>开发作者：汤莉</li>
 * <li>设计日期：2010-8-29；时间：下午05:36:44</li>
 * <li>类型名称：DateFieldFactory</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class DateFieldFactory {

	/**
	 * 
	 * <ul>
	 * <li>方法含义：创建日期控件</li>
	 * <li>方法作者：汤莉</li>
	 * <li>编写日期：2010-8-29；时间：下午下午05:37:36</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param gxtComponentMetaModel
	 * @return
	 */
	public static DateField createDateField(GxtComponentMetaNodeModel gxtComponentMetaModel){
		return configureDateField(gxtComponentMetaModel);
	}
	
	/**
	 * 
	 * <ul>
	 * <li>方法含义：创建</li>
	 * <li>方法作者：汤莉</li>
	 * <li>编写日期：2010-8-29；时间：下午下午05:38:57</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param gxtComponentMetaNodeModel
	 * @return
	 */
	private static DateField configureDateField(GxtComponentMetaNodeModel gxtComponentMetaNodeModel)
	{
		final DateField date=createDefaultDateField();
//		date = AbstractComponetFactory.configureAbstractComponet(date, gxtComponentMetaNodeModel);
		 AbstractComponetFactory.configureAbstractComponet(date, gxtComponentMetaNodeModel);
		
		//数据加载
		if(gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.GXTTOSEAMSERVICEPARAMETERMODEL.name().toLowerCase())!=null){
			final ServiceInvokerAsync serviceInvoker = GWT.create(ServiceInvoker.class);
			final GxtToSeamServiceParameter gxtToSeamServiceParameter = date.getData("gxtToSeamServiceParameter");

			/*final GxtToSeamServiceParameter gxtToSeamServiceParameter = new GxtToSeamServiceParameter();
			gxtToSeamServiceParameter.setServiceComponentName("com.test.server.business.PersonService");
			gxtToSeamServiceParameter.setServiceMethodName("loadDateFieldData");
//			gxtToSeamServiceParameter.setServiceMethodName("getList2");
//			gxtToSeamServiceParameter.setServiceMethodName("getEnums");
			BaseModel[] methodArguments2 = new BaseModel[1];
			methodArguments2[0] = new BaseModel();
			gxtToSeamServiceParameter.setMethodArguments(methodArguments2 );*/
			
			serviceInvoker.callService(gxtToSeamServiceParameter,
					new AsyncCallback<GxtToSeamServiceResult>() {

						@Override
						public void onSuccess(GxtToSeamServiceResult result) {
							//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
							List<Long> bmBaseModels = new LinkedList<Long>();
							ModelData modelData = result.getResult();
							bmBaseModels = modelData.get("listOfModelData");
							
								//Date d = new Date(Long.parseLong(bmBaseModels.get(0)));
							Date d = new Date(bmBaseModels.get(0));
							//System.err.println("$$$$$$$$$" + bmBaseModels.get(0));
								date.setValue(d);		
						}

						@Override
						public void onFailure(Throwable caught) {
							System.out.println("#### Rpc Exception is ####"+caught +"When get the List<GxtComponentMetaNodeModel>");
							caught.printStackTrace();
						}
					});
			
		}
		//数据加载
		if(gxtComponentMetaNodeModel.get(TextFieldModelEnum.DATALOAD.name().toLowerCase())!=null){
			final ServiceInvokerAsync serviceInvoker = GWT.create(ServiceInvoker.class);
			final GxtToSeamServiceParameter gxtToSeamServiceParameter = date.getData("gxtToSeamServiceParameter");

			/*final GxtToSeamServiceParameter gxtToSeamServiceParameter = new GxtToSeamServiceParameter();
			gxtToSeamServiceParameter.setServiceComponentName("com.test.server.business.PersonService");
			gxtToSeamServiceParameter.setServiceMethodName("loadDateFieldData");
//			gxtToSeamServiceParameter.setServiceMethodName("getList2");
//			gxtToSeamServiceParameter.setServiceMethodName("getEnums");
			BaseModel[] methodArguments2 = new BaseModel[1];
			methodArguments2[0] = new BaseModel();
			gxtToSeamServiceParameter.setMethodArguments(methodArguments2 );*/
			
			serviceInvoker.callService(gxtToSeamServiceParameter,
					new AsyncCallback<GxtToSeamServiceResult>() {

						@Override
						public void onSuccess(GxtToSeamServiceResult result) {
							//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
							List<Long> bmBaseModels = new LinkedList<Long>();
							ModelData modelData = result.getResult();
							bmBaseModels = modelData.get("listOfModelData");
							
								//Date d = new Date(Long.parseLong(bmBaseModels.get(0)));
							Date d = new Date(bmBaseModels.get(0));
							//System.err.println("$$$$$$$$$" + bmBaseModels.get(0));
								date.setValue(d);		
						}

						@Override
						public void onFailure(Throwable caught) {
							System.out.println("#### Rpc Exception is ####"+caught +"When get the List<GxtComponentMetaNodeModel>");
							caught.printStackTrace();
						}
					});
			
		}
		if(gxtComponentMetaNodeModel.get(DateFieldModelEnum.FORMATVALUE.name().toLowerCase())!=null){
			//设置日期格式
			date.setFormatValue((Boolean)gxtComponentMetaNodeModel.get(DateFieldModelEnum.FORMATVALUE.name().toLowerCase()));
		
		}
		if(gxtComponentMetaNodeModel.get(DateFieldModelEnum.FIELDLABEL.name().toLowerCase())!=null){
			//设置标签
			date.setFieldLabel((String)gxtComponentMetaNodeModel.get(DateFieldModelEnum.FIELDLABEL.name().toLowerCase()));
		
		}
		if(gxtComponentMetaNodeModel.get(DateFieldModelEnum.READONLY.name().toLowerCase())!=null){
			//设置是否为只读
			date.setReadOnly((Boolean)gxtComponentMetaNodeModel.get(DateFieldModelEnum.READONLY.name().toLowerCase()));
		
		}
		if(gxtComponentMetaNodeModel.get(DateFieldModelEnum.MAXVALUE.name().toLowerCase())!=null){
			//设置日期的最大值
			date.setMaxValue((Date)gxtComponentMetaNodeModel.get(DateFieldModelEnum.MAXVALUE.name().toLowerCase()));
		
		}
		if(gxtComponentMetaNodeModel.get(DateFieldModelEnum.MINVALUE.name().toLowerCase())!=null){
			//设置日期的最小值
			date.setMinValue((Date)gxtComponentMetaNodeModel.get(DateFieldModelEnum.MINVALUE.name().toLowerCase()));
		
		}
		if(gxtComponentMetaNodeModel.get(DateFieldModelEnum.NAME.name().toLowerCase())!=null){
			//设置控件名
			date.setName((String)gxtComponentMetaNodeModel.get(DateFieldModelEnum.NAME.name().toLowerCase()));
		
		}
		if(gxtComponentMetaNodeModel.get(DateFieldModelEnum.ALLOWBLANK.name().toLowerCase())!=null){
			//设置控件是否允许为空
			date.setAllowBlank((Boolean)gxtComponentMetaNodeModel.get(DateFieldModelEnum.ALLOWBLANK.name().toLowerCase()));
		
		}
		
		return date;
	}
	
	/**
	 * 
	 * <ul>
	 * <li>方法含义：创建默认的日期控件</li>
	 * <li>方法作者：汤莉</li>
	 * <li>编写日期：2010-8-29；时间：下午下午05:40:16</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @return
	 */
	private static DateField createDefaultDateField()
	{
		DateField date=new DateField();
		//date.setValue(new Date());
		date.setFieldLabel("当前日期");
		return date;
	}
}
